package cn.highedu.http;


import java.net.Socket;

/**
 * 客户端服务线程
 */
public class ClientHandler implements Runnable{
    private Socket socket;

    public ClientHandler(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //解析请求
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);
            String uri = request.getUri();

            //处理请求
            DispatcherServlet servlet = DispatcherServlet.getInstance();
            servlet.service(request, response);

            //发送响应
            response.send();

            socket.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
